package ym.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ym.mapper.CategoryMapper;
import ym.pojo.Category;
import ym.service.CategoryService;

import java.util.List;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public void add(Category category) {
        categoryMapper.insert(category);
    }

    @Override
    public void update(Category category) {
        categoryMapper.updateById(category);
    }

    @Override
    public void delete(Integer id) {
        categoryMapper.deleteById(id);
    }

    @Override
    public Category findById(Integer id) {
        return categoryMapper.selectById(id);
    }

    @Override
    public void updateStatus(Integer id) {
        Category category = categoryMapper.selectById(id);
        if (category.getCategoryStatus().equals("1")) {
            category.setCategoryStatus("0");
        } else {
            category.setCategoryStatus("1");
        }
        categoryMapper.updateById(category);
    }

    @Override
    public List<Category> findAll() {
        return categoryMapper.selectList(null);
    }

    @Override
    public List<Category> findAllByTypeWithStatus(Integer categoryTypeId) {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("categoryTypeId", categoryTypeId).eq("categoryStatus", "1");
        return categoryMapper.selectList(queryWrapper);
    }

    @Override
    public Page<Category> search(int page, int size) {
        return categoryMapper.selectPage(new Page<>(page, size), null);
    }

    @Override
    public Page<Category> searchByType(Integer categoryTypeId, int page, int size) {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("categoryTypeId", categoryTypeId);
        return categoryMapper.selectPage(new Page<>(page, size), queryWrapper);
    }


    @Override
    public Page<Category> searchByTypeWithStatus(Integer categoryTypeId, int page, int size) {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("categoryTypeId", categoryTypeId).eq("categoryStatus", "1");
        return categoryMapper.selectPage(new Page<>(page, size), queryWrapper);
    }
}
